package services;

//import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import common.Configuration;
import models.EmailDTO;


/**
 *
 * @author 	Sami Diab 
 * @since	15/11/2011
 */
public class EmailServiceImpl implements IEntity {

    Statement stmt = null;
    ResultSet rst = null;
    private EmailDTO email;
    
    
    public EmailDTO GetById(int id) {
        try {
        	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from email where idEmail=" + id);
            email.setidEmail(id);
            email.setEmail(rst.getString("email"));

            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(EmailServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return email;
    }
   
    public boolean EmailExists(String emailAccount)
    {
    	boolean exists = false;
    	
        try {	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from email where email='" + emailAccount + "'");
        	exists = rst.wasNull();
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(EmailServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return exists;
    }
    
    public EmailDTO GetByEmail(String emailAccount) {
        try {
        	
        	email = new EmailDTO();
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from email where email='" + emailAccount +"'");
            email.setidEmail(rst.getInt("idEmail"));
            email.setEmail(rst.getString("email"));

            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(EmailServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return email;
    }
    
    public List<EmailDTO> GetAll() {
    	
    	List<EmailDTO> emailList = new ArrayList<EmailDTO>();
    	
        try {       	
        	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from email ");
            //rst.beforeFirst();
            while (rst.next()) {
            	email = new EmailDTO();
                email.setidEmail(rst.getInt("idEmail"));
                email.setEmail(rst.getString("email"));
                emailList.add(email);
            }
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(EmailServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return emailList;
    }

    public void Add(Object obj){
    	try {
    		
    		EmailDTO objCast=(EmailDTO)obj;
    		stmt = Configuration.GetStatement();
    		stmt.execute("INSERT INTO dbsurvey.email (email)  VALUES ('" + objCast.getEmail() + "')");
    		Configuration.CloseConnection();
    		
    	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void Update(Object obj){
    	
    	try {
    		
    		EmailDTO objCast=(EmailDTO)obj;
    		stmt = Configuration.GetStatement();
    		stmt.execute("update dbsurvey.email set email=" + objCast.getEmail() + " where idEmail=" + objCast.getidEmail());
    		Configuration.CloseConnection();
    		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void Remove(int id){
        try {
        	
        	stmt = Configuration.GetStatement();
        	stmt.execute("delete from email where idEmail =" + id);
        	Configuration.CloseConnection();
        
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public void Remove(String email){
        try {
        	
        	stmt = Configuration.GetStatement();
        	stmt.execute("delete from `dbsurvey`.`email` where `email` ='" + email + "';");
        	Configuration.CloseConnection();
        
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
